#include "qtmusic.h"
#include "ui_qtmusic.h"
#include <QGraphicsDropShadowEffect>


QtMusic::QtMusic(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::QtMusic)
{
    ui->setupUi(this);

    // 初始化UI界面的操作
    initUi();

    // 处理所有信号和槽的连接
    connectSignalAndSlots();
}

QtMusic::~QtMusic()
{
    delete ui;
}

void QtMusic::initUi()
{
    // 设置程序图标
    QIcon icon(":/images/tubiao");
    this->setWindowIcon(icon);

    // 设置窗口无标题栏
    this->setWindowFlag(Qt::FramelessWindowHint);

    // 设置窗口背景透明
    this->setAttribute(Qt::WA_TranslucentBackground);
    // 给窗口设置阴影效果
    QGraphicsDropShadowEffect* shadowEffect = new QGraphicsDropShadowEffect(this);
    shadowEffect->setOffset(0, 0);
    shadowEffect->setColor("#000000");
    shadowEffect->setBlurRadius(10);
    this->setGraphicsEffect(shadowEffect);

    // 设置BtForm图表 & 文本
    ui->recommend->setIconAndText(":/images/recommend.png", "推荐", 0);
    ui->audio->setIconAndText(":/images/radio.png", "电台", 1);
    ui->music->setIconAndText(":/images/music.png", "音乐馆", 2);
    ui->like->setIconAndText(":/images/like_1.png", "我喜欢", 3);
    ui->local->setIconAndText(":/images/local.png", "本地下载", 4);
    ui->recent->setIconAndText(":/images/recent.png", "最近播放", 5);

    // 本地下载BtForm动画默认显示
    ui->local->setBg();
    ui->local->showAnimation();
    ui->stackedWidget->setCurrentIndex(4);
}

void QtMusic::mouseMoveEvent(QMouseEvent *event)
{
    // 拦截鼠标左键单击事件
    if(Qt::LeftButton == event->buttons())
    {
        move(event->globalPos() - dragPosition);
    }
}

void QtMusic::mousePressEvent(QMouseEvent *event)
{
    // 拦截鼠标左键单击事件
    if(event->button() == Qt::LeftButton)
    {
        // 获取鼠标相对于屏幕左上角的全局坐标
        dragPosition = event->globalPos() - geometry().topLeft();
        /*qDebug() << event->globalPos();
        qDebug() << geometry().topLeft();
        qDebug() << dragPosition;*/
    }

    QWidget::mousePressEvent(event);
}

void QtMusic::connectSignalAndSlots()
{
    // 将BtForm类的btClick信号，与QtMusic中的onBtFormClick槽函数进行关联
    connect(ui->recommend, &BtForm::btClick, this, &QtMusic::onBtFormClick);
    connect(ui->audio, &BtForm::btClick, this, &QtMusic::onBtFormClick);
    connect(ui->music, &BtForm::btClick, this, &QtMusic::onBtFormClick);
    connect(ui->like, &BtForm::btClick, this, &QtMusic::onBtFormClick);
    connect(ui->local, &BtForm::btClick, this, &QtMusic::onBtFormClick);
    connect(ui->recent, &BtForm::btClick, this, &QtMusic::onBtFormClick);
}


void QtMusic::on_quit_clicked()
{
    close();
}

void QtMusic::onBtFormClick(int pageId)
{
    // 1、获取当前所有页面所有btForm按钮类型的对象
    QList<BtForm*> buttonList = this->findChildren<BtForm*>();

    // 2、遍历所有对象，如果不是当前id的按钮，则把之前设置的背景颜色清除
    foreach (BtForm* btItem, buttonList) {
        if (pageId != btItem->PageId()) {
            btItem->clearBg();
            btItem->hideAnimation();
        } else {
           btItem->showAnimation();
        }
    }

    // 3、设置当前对应的页面
    ui->stackedWidget->setCurrentIndex(pageId);
}
